const { Controller } = require("egg");
class DirectoryController extends Controller {
  async list() {
    const { service } = this;
    try {
      const result = await service.directory.list();
      this.ctx.helper.success(result);
    } catch (error) {
      this.ctx.helper.error(error);
    }
  }
  async create() {
    const { ctx, service } = this;
    const { name, parentId, level, creator } = ctx.request.body;
    console.log("creator", creator)
    try {
      const result = await service.directory.create({
        name,
        parentId,
        level,
        creator
      });
      this.ctx.helper.success(result);
    } catch (error) {
      this.ctx.helper.error(error);
    }
  }

  async delete() {
    const { ctx, service } = this;
    const { id } = ctx.request.body;
    try {
      const result = await service.directory.delete(id);
      this.ctx.helper.success(result);
    } catch (error) {
      this.ctx.helper.error(error);
    }
  }

  async update() {
    const { ctx, service } = this;
    try {
      const result = await service.directory.update(ctx.request.body);
      this.ctx.helper.success(result);
    } catch (error) {
      this.ctx.helper.error(error);
    }
  }

  async updateStatus() {
    const { ctx, service } = this;
    const { id, status } = ctx.request.body;
    if (!id) {
      return this.ctx.helper.error("参数异常");
    }
    try {
      const result = await service.directory.updateStatus({
        id,
        status,
      });
      this.ctx.helper.success(result);
    } catch (error) {
      this.ctx.helper.error(error);
    }
  }

  async setNextOrder() {
    const { ctx, service } = this;
    const { id } = ctx.request.body;
    if (!id) {
      return this.ctx.helper.error("参数异常");
    }
    try {
      const result = await service.directory.setNextOrder({
        id,
      });
      this.ctx.helper.success(result);
    } catch (error) {
      this.ctx.helper.error(error);
    }
  }

  async setPrevOrder() {
    const { ctx, service } = this;
    const { id } = ctx.request.body;
    if (!id) {
      return this.ctx.helper.error("参数异常");
    }
    try {
      const result = await service.directory.setPrevOrder({
        id,
      });
      this.ctx.helper.success(result);
    } catch (error) {
      this.ctx.helper.error(error);
    }
  }

  async navList() {
    const { service } = this;
    try {
      const result = await service.directory.navList();
      this.ctx.helper.success(result);
    } catch (error) {
      this.ctx.helper.error(error);
    }
  }
}

module.exports = DirectoryController;
